package com.haipumi.servlets;

import java.io.IOException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
  
import javax.servlet.ServletConfig;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

import com.haipumi.utils.CommonData;
  
public class LongTimeConServlet extends HttpServlet {  
  
    private static final long serialVersionUID = -3981794330055840248L;  
  
    private String interval = "1";  
  
    public void init(ServletConfig config) throws ServletException {  
        this.interval = config.getInitParameter("interval");  
        super.init();  
    }  
  
    public void destroy() {  
        this.interval = null;  
        super.destroy();  
    }  
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,  
            java.io.IOException {  
//    	 int old_date_str= (Integer) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKCOUNT);
//        for (int i = 0; i < 1000; i++) {  //循环1000次  也就是 3000秒 50分钟 也就是在首页会和服务器建立50分钟的长接连
//            try {  
//                Thread.sleep(3000 * Integer.valueOf(interval));  
//            } catch (NumberFormatException e) {  
//                e.printStackTrace();  
//            } catch (InterruptedException e) {  
//                e.printStackTrace();  
//            }  
//              
//            int date_str= (Integer) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKCOUNT);
//            if(old_date_str>date_str){
//            	old_date_str=date_str;
//            	writerResponse(response, date_str, "showServerTime");// msg是test.jsp中的那个js方法的名称
//            }
//        }
        return;  
    }  
  
    protected void writerResponse(HttpServletResponse response, int body, String client_method) throws IOException {  
        StringBuffer sb = new StringBuffer();  
//      //CDATA是xml里的东西，用在这里的目的是为了让xhtml校验通过，因为在xml中，
        //CDATA里的内容是“文本”，不需要转义，可以存在<>&等在xml中不允许出现的符号。
        sb.append("<script type=\"text/javascript\">//<![CDATA[\n");  
        //<script type="text/javascript">//<![CDATA[
        //parent.showServerTime(body);
        //]]></script>

        sb.append("     parent.").append(client_method).append("(\"").append(body).append("\");\n");  
        sb.append("//]]></script>");  
  
        response.setContentType("text/html;charset=UTF8");  
        response.addHeader("Pragma", "no-cache");  
        response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");  
        response.setHeader("Cache-Control", "pre-check=0,post-check=0");  
        response.setDateHeader("Expires", 0); //防止JSP或者Servlet中的输出被浏览器保存在缓冲区中。
        response.getWriter().write(sb.toString());  
        response.flushBuffer();  
    }  
  
    public String getInterval() {  
        return interval;  
    }  
  
    public void setInterval(String interval) {  
        this.interval = interval;  
    }  
  
}  